<?php 

class Block {
	private $bId = '';
	private $bIdType = '';
	private $bType = '';
	private $bTypeExtension = '';
	private $bTypeExtensionName = '';
	private $bName = '';
	private $bClass = '';
	private $html = '';
	private $bParams = '';
	private $bMaxArrKeyParams = 2;
	
	function __construct($bId) {
		global $_DB, $_PAGE;
		$this->bId = $bId;
		
		//$sql = "SELECT B.name, B.class, T.label type, T.id FROM blocks B, blocks_type T WHERE B.id = '".$bId."' AND T.id = B.id_type";
		$sql = "SELECT B.name, B.class, T.label type, T.id, T.extension, T.extension_name, P.params 
				FROM 	".$_DB->getPrefix()."blocks B, 
						".$_DB->getPrefix()."blocks_type T, 
						".$_DB->getPrefix()."blocks_page P 
				WHERE 
						B.id = '".$bId."' 
					AND T.id = B.id_type 
					AND P.id_block = B.id 
					AND P.id_page = '".$_PAGE->pId."'";

		$r = $_DB->query($sql);
			
		$this->bType = $r[0]['type']; 
		$this->bIdType = $r[0]['id'];
		$this->bTypeExtension = $r[0]['extension'];
		$this->bTypeExtensionName = $r[0]['extension_name'];
		$this->bName = $r[0]['name'];
		$this->bClass = $r[0]['class'];
		$this->bParams = (!is_null($r[0]['params']) ? $r[0]['params'] : '');
				
		$this->loadHtml();	
	}
	
	function get ($field = null) {
		switch ($field) {
    			case 'name': return $this->bName; break;
    			case 'class': return $this->bClass; break;
    			case 'params': return $this->bParams; break;
    			default: return null;
		}
	}
	
	function html() {
		return $this->html;
	}
	
	private function loadHtml () {
		if ($this->bType == 'free_html' OR $this->bType == 'container' OR $this->bType == 'static'){
			$this->loadHtmlStatic();
		}
		
		if ($this->bType == 'dinamic'){
			$this->loadHtmlDinamic();
		}
		
	}
	
	private function loadHtmlStatic () {
		global $_TRANSLATE, $_SITE;
		
		$fileBlockStatic = $_SITE['path']['dir']['blocks']['static'].'/'.$this->bId.'.tag.html';
		
		//search into ext
		if (!is_file($fileBlockStatic)) {
			
			$fileBlockStatic = $_SITE['path']['dir']['blocks'][$this->bTypeExtensionName][$this->bType].'/'.$this->bId.'.tag.html';
			
			$description = 'search html file, for static block ('.$this->bName.'), in extension '.$this->bTypeExtensionName;
			$log = new Log('Block', 'Block.php', $description);
			$log->insert();
		}
		
		if (is_file($fileBlockStatic)) {
			$this->html = file_get_contents($fileBlockStatic);
		} else {
			$description = 'html file for static block '.$this->bName.' is not found';
			$log = new Log('Block', 'Block.php', $description);
			$log->insert();
			$this->html = '<!-- '.$this->bName.'-file-html-not-found -->';
		}
	}
	
	private function loadHtmlDinamic () {
		global $_USER;
		
		switch ($this->get('class')) {
			case 'CardProfessionista':
			case 'HeadPage':
			case 'ListProfessionisti':
			case 'ListProfessionistiColRight':
			case 'Pager':
    		case 'RenderForm':
    		case 'Breadcrumb':
    			$className = $this->get('class');
    			$params = $this->get('params');
 
    			if ($params != '') {
    				$obj = new $className($params);
    			} else {
    				$obj = new $className();	
    			}
    				
    			$this->html = $obj->html();
    		break;
    		
    		case 'Menu':
    			$className = $this->get('class');
    			$params = $this->get('params');
    			
    			if ($params != '') {
    				$par = explode(',', $params);		
    				$obj = new $className($par[0], $par[1], $par[2]);
    			} else {
    				$obj = new $className();
    			}
    			$this->html = $obj->html();
    		break;
    		
    		case 'User':
    			if ($_USER->login){
    				$this->html = $_USER->html();
    			} else {
    				$this->loadHtmlStatic();
    			}
    		break;
    		
    		default:
    			$description = 'class dinamic block '.$this->bName.' is not found';
				$log = new Log('Block', 'Block.php', $description);
				$log->insert();
				$this->html = '<!-- '.$this->bName.'-file-html-not-found -->';
    		break; 
		}
		//$this->html = $obj->html();
	}
	
	/*
	 * Return an array with children blocks id or NULL
	 * */
	private function children () {
		$arr = array();
		$this->bId;
		
		$sql = "SELECT id FROM ".$_DB->getPrefix()."blocks WHERE parent = '".$this->bId."'";
		$r = $_DB->query($sql);
		
		foreach ($r as $b) {
			$arr[] = $b['id'];
		}
		
		if (count($arr) > 0) {
			return $arr;
		}
		
		return NULL;
	}
}
/*		//load blocks list 
		$blocks = $_DB->query("SELECT id_block FROM blocks_page WHERE id_page = '".$pId."' ORDER BY sort");
		foreach ($blocks as $block) {
			$this->blocksList[] = $block['id_block'];
		}
*/
?>